package com.sky.mapper;

import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrdersMapper {
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into orders ( number, status, user_id, address_book_id, order_time, checkout_time, pay_method, amount, remark, phone, address, user_name, consignee)" +
            "values ( #{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod}, #{amount}, #{remark}, #{phone}, #{address}, #{userName}, #{consignee})")
    void insert(Orders order);

    /**
     * 历史订单分页查询
     * @param ordersPageQueryDTO
     * @return
     */

    List<OrderVO> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /**
     * 修改订单状态
     * @param id
     * @param status
     * @param cancelTime
     * @param cancelReason
     */
//    @Update("update orders set status = #{status}, cancel_time = #{cancelTime}, cancel_reason = #{cancelReason} where id = #{id}")
    void updateStatus(Long id, Integer status, LocalDateTime cancelTime, String cancelReason);

    /**
     * 统计订单数量
     * @param status
     * @return
     */
    @Select("select count(*) from orders where status = #{status}")
    Integer countStatus(Integer status);
    /**
     * 统计统一时间订单数量
     */
    Integer countByTimeAndStatus(Integer status,LocalDateTime begin, LocalDateTime end);

    /**
     * 取消超时订单
     * @param String
     * @param time
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> cancelOrder(String status, LocalDateTime time);

    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 修改订单状态
     * @param orderStatus
     * @param orderPaidStatus
     * @param check_out_time
     * @param id
     */

    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{check_out_time} where id = #{id}")
    void updateStatuswx(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, Long id );

    /**
     * 根据订单状态统计订单数量
     * @param begin
     * @param end
     * @param status
     * @return
     */
    @Select("select sum(amount) from orders where order_time between #{begin} and #{end} and status = #{status}")
    Double totalAmount(LocalDateTime begin, LocalDateTime end, Integer status);

    /**
     * 统计销量排名top10菜品的名称
     * @param begin
     * @param end
     * @return
     */
    @Select("select od.name name, sum(od.number) number from orders o , order_detail od where o.id = od.order_id and o.status = 5 and o.order_time between #{begin} and #{end}  GROUP BY od.name ORDER BY sum(od.number) desc LIMIT 10;")
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);
}
